﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVC.Web.UI.Accordion;
using System.Web.Mvc;

namespace MVC.Web.UI.Accordion
{
    public class WebAccordionBuilder :IControlBuilder<WebAccordionBuilder>
    {
        private WebAccordion _webAccordion = null;
        private HtmlHelper _helper = null;

        public WebAccordionBuilder(HtmlHelper helper) 
        {
            this._helper = helper;
            _webAccordion = new WebAccordion(helper);

        }

        #region Methods 

        public WebAccordionBuilder Items(Action<ContentItemBuilder> action) 
        {
            ContentItemBuilder builder = new ContentItemBuilder(this._webAccordion.AccordionItems);
            action.Invoke(builder);
            return this;
        }

        /// <summary>
        /// Set the panel index that will be activated
        /// </summary>
        public WebAccordionBuilder Activate(int index) 
        {
         
            if (index > this._webAccordion.AccordionItems.Count)
                throw new IndexOutOfRangeException("Such index does not exist");
            _webAccordion.Activate = index;
            return this;
        }

        /// <summary>
        /// Choose your favorite animation, 
        /// or disable them (set to false). In addition to the default, 'bounceslide' and all defined easing methods are supported ('bounceslide' requires UI Effects Core).
        /// </summary>
        public WebAccordionBuilder Animated(string amimationType) 
        {
            //Will be implemented on next iteration 
            return this;
        }

        /// <summary>
        /// set, the highest content part is used as height reference for all other parts. Provides more consistent animations.
        /// Default value is true
        /// </summary>
        public WebAccordionBuilder AutoHeight(bool autoHeight) 
        {
            this._webAccordion.AutoHeight = autoHeight;
            return this;
        }


        /// <summary>
        /// If set, clears height and overflow styles after finishing animations.
        /// This enables accordions to work with dynamic content. Won't work together with autoHeight.
        /// Default value is false
        /// </summary>
        public WebAccordionBuilder ClearStyle(bool clearStyle)
        {
            if ((this._webAccordion.AutoHeight == true)&&(clearStyle == true))
                throw new ArgumentException("Set AutoHeight to false before use ClearStyle to set it to true");
            this._webAccordion.ClearStyle = clearStyle;
            return this;
        }

        /// <summary>
        /// Whether all the sections can be closed at once. 
        /// Allows collapsing the active section by the triggering event "click" is the default.
        /// </summary>
        public WebAccordionBuilder Collapsible(bool collapsible) 
        {
            this._webAccordion.Collapsible = collapsible;
            return this; 
        }

        /// <summary>
        /// The event on which to trigger the accordion.
        /// </summary>
        public WebAccordionBuilder EventTrigger()  
        {
            //this needs to be implemented on next iteration
            return this;
        }

        /// <summary>
        /// If set, the accordion completely fills the height of the parent element. Overrides autoheight.
        /// </summary>
        public WebAccordionBuilder FillSpace(bool fillSpace) 
        {
            this._webAccordion.FillSpace = fillSpace;
            return this;
        }

        private string ConfigureTab()
        {
            // Create tag builder
            var scriptBuilder = new TagBuilder("script");
            scriptBuilder.MergeAttribute("type", "text/javascript");

            StringBuilder innerScriptBlock = new StringBuilder();
            innerScriptBlock.Append("\n\t$(function(){");
            innerScriptBlock.Append("\n\t  $('#" + this._webAccordion.Name + "').accordion({");

            //"\n\t\tanimated: 'slide' -  this will be implemented on next iteration
            // "\n\t\tevent: "click"
            innerScriptBlock.Append(     
		        "\n\t\tactive: "+ this._webAccordion.Activate +","+
		        "\n\t\tautoHeight: "+ this._webAccordion.AutoHeight.ToString().ToLower() +","+
		        "\n\t\tclearStyle: " +this._webAccordion.ClearStyle.ToString().ToLower()+","+
		        "\n\t\tcollapsible: "+ this._webAccordion.Collapsible.ToString().ToLower()+","+
		        "\n\t\tfillSpace: "+ this._webAccordion.FillSpace.ToString().ToLower()          
           );
            innerScriptBlock.Append("});");

            //Map all registered handlers to the client side events of the control    
            //foreach (KeyValuePair<TabEvents, string> item in this._webTab.TabEventHandlers)
            //{
            //    innerScriptBlock
            //   .Append("\n\t  $('#" + this._webTab.Name + "').bind('" + ((TabEvents)item.Key).GetStringValue() + "'," + item.Value + ")");
            //}

            innerScriptBlock.Append("\n\t});\n ");
            scriptBuilder.InnerHtml = innerScriptBlock.ToString();
            return "<!--  //-->" + "\n    " + scriptBuilder.ToString(TagRenderMode.Normal);
        }


        #endregion


        public WebAccordionBuilder Name(string name)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))
                throw new ArgumentNullException("Provide a valid name for the control");
            this._webAccordion.Name = name;
            this._webAccordion.Attributes.Add("id", name);
            return this;
        }

        public void Render()
        {
            this._webAccordion.Scripts = ConfigureTab();
            _webAccordion.RenderControl(this._helper.ViewContext);    
        }
    }
}
